// axios的封装处理
import axios from "axios";
import { clearToken, getToken } from "./token";
import { message as _message } from "antd";

const request=axios.create({
    baseURL:'http://120.26.115.44:8501',
    timeout:5000
})
// 添加请求拦截器
request.interceptors.request.use(config=>{
    // 给header添加token
    const token=getToken('token_key')
   if(token) config.headers.token=token
    return config
},error=>{
    return Promise.reject(error)
})

// 添加响应拦截器
request.interceptors.response.use(response=>{
    console.log(response);
    let {message}=response.data
    if(message.includes('token')){
        _message.error(message)
        clearToken()
        window.location.reload()
        return 
    }
    return response
},error=>{
    let {code,message}=error
    if(code=='ECONNABORTED'){
        _message.error(message)
        clearToken()
        window.location.reload()
    }
   
    return error
})

// 导出request
export default request